#include <stdio.h>
#include <assert.h>

static int factorials[] = {
  1,
  1,
  2, /* 2! */
  6,
  24,
  120,
  720,
  5040,
  40320,
  362880, /* 9! */
};

static int is_very_special(int num) {
  char buf[32];
  size_t len;

  len = snprintf(buf, sizeof(buf), "%d", num);
  assert(len < sizeof(buf));

  buf[len] = 0;

  int sum = 0;
  int i;
  for (i = 0; i < len; i++) {
    sum += factorials[buf[i] - '0'];
    if (sum > num)
      return 0;
  }

  if (sum != num)
    return 0;
  else
    return 1;
}

int main(int argc, char **argv) {
  int i;

  for (i = 0; i < 1000000; i++) {
    if (is_very_special(i))
      printf("%d\n", i);
  }

  return 0;
}
